﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DBLOG.BizTalk.BizTalkExplorerServices.Core;
using Microsoft.BizTalk.ExplorerOM;
using Microsoft.BizTalk.Operations;

namespace DBLOG.BizTalk.BizTalkExplorerServices.Explorers
{
    public class OrchestrationExplorer : BizTalkExplorerBase
    {
        public List<BtsOrchestration> GetOrchestrations()
        {
            List<BtsOrchestration> orchestrations = new List<BtsOrchestration>();

            ApplicationCollection applicationCollection = this._explorer.Applications;

            foreach (Application application in applicationCollection)
            {
                foreach (BtsOrchestration btsOrchestration in application.Orchestrations)
                {
                    orchestrations.Add(btsOrchestration);
                }
            }

            return orchestrations;
        }

        public List<BtsOrchestration> GetDisabledOrchestrations()
        {
            List<BtsOrchestration> disabledOrchestrations = new List<BtsOrchestration>();

            ApplicationCollection applicationCollection = this._explorer.Applications;

            foreach (Application application in applicationCollection)
            {
                foreach (BtsOrchestration btsOrchestration in application.Orchestrations)
                {
                    if (btsOrchestration.Status != OrchestrationStatus.Started)
                        disabledOrchestrations.Add(btsOrchestration);
                }
            }

            return disabledOrchestrations;
        }

        public List<Instance> GetSuspendedOrchestrationInstances()
        {
            var suspendedOrchestrationInstances = from o in
                                                  this._operations.GetServiceInstances().Cast<Instance>().ToList<Instance>()
                                                  where o.InstanceStatus == InstanceStatus.Suspended ||
                                                        o.InstanceStatus == InstanceStatus.SuspendedAll ||
                                                        o.InstanceStatus == InstanceStatus.SuspendedNotResumable
                                                  select o;

            return suspendedOrchestrationInstances.ToList<Instance>();
        }
    }
}